package com.sardine.base.esm.support;

import com.sardine.base.esm.session.EsmSession;
import com.sardine.base.esm.session.EsmSessionFactory;
import com.sardine.base.esm.session.EsmSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.support.DaoSupport;

import static org.springframework.util.Assert.notNull;

/**
 * 上海京东到家元信信息技术有限公司<br>
 * Created with IntelliJ IDEA.
 * Date:2017/5/23
 * Time:16:16
 * EsmSessionDao 超类，是DAO的具体实现的超类
 * @author daifeng<br>
 *         daifeng@imdada.cn
 * @since JDK 1.7 <br>
 * Copyright (c) 2017 imdada System Incorporated All Rights Reserved.
 */
public abstract class EsmSessionDaoSupport extends DaoSupport{
    private static final Logger logger = LoggerFactory.getLogger(EsmSessionDaoSupport.class);
    /**
     * session
     */
    private EsmSession esmSession;
    //判断是否使用外部配置
    private boolean externalEsmSession;

    /**
     * 动态设置  EsmSessionFactory 在scanner 扫描的时候，通过spring注入到这个类中
     *
     * @param esmSessionFactory
     */
    public void setEsmSessionFactory(EsmSessionFactory esmSessionFactory) {
        if (!this.externalEsmSession) {
            this.esmSession = new EsmSessionTemplate(esmSessionFactory);
        }
    }

    /**
     *
     *
     * 动态设置  EsmSessionTemplate 在scanner 扫描的时候，通过spring注入到这个类中
     *
     * @param esmSessionTemplate
     */
    public void setEsmSessionTemplate(EsmSessionTemplate esmSessionTemplate) {
        this.esmSession = esmSessionTemplate;
        this.externalEsmSession = true;
    }


    public EsmSession getEsmSession() {
        return this.esmSession;
    }

    /**
     * 检查DAO的配置。必须满足有  esmSession 信息，该方法在属性加载完成后调用
     */
    protected void checkDaoConfig() {
        notNull(this.esmSession, "Property 'esmSessionFactory' or 'esmSessionTemplate' are required");
    }
}
